package com.yf1706.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yf1706.bean.FloatSalary;
import com.yf1706.bean.FloatSalarySob;
import com.yf1706.service.FloatSalaryService;
import com.yf1706.service.SocialSecurityService;
import com.yf1706.tools.JsonTool;
import com.yf1706.vo.FloatSalaryVO;

@Controller
@RequestMapping("floatSalary")
public class FloatSalaryController extends BaseController{

	@Autowired
	private FloatSalaryService floatSalaryService;
	
	@Autowired
	private SocialSecurityService securityService;
	
	//--------------------浮动工资账套-------------------------
	@RequestMapping("querySobByPage")
	@ResponseBody
	public Map<String, Object> querySobByPage(int page,int rows,String sort,String order) {
		Page<FloatSalarySob> pages = floatSalaryService.querySobByPage(page, rows, sort, order);
		Long total= pages.getTotalElements();
		List<FloatSalarySob> list = pages.getContent();
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	
	@RequestMapping("findSobByPage")
	@ResponseBody
	public Map<String, Object> findSobByPage(String floatSalarySobName,int page,int rows,String sort,String order) {
		Page<FloatSalarySob> pages = floatSalaryService.findSearch(floatSalarySobName, page, rows, sort, order);
		Long total= pages.getTotalElements();
		List<FloatSalarySob> list = pages.getContent();
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	
	
	@RequestMapping("insertSob")
	@ResponseBody
	public void insertSob(HttpServletRequest request,HttpServletResponse response) {
		JSONObject jObject = JSON.parseObject(request.getParameter("djt_FSsob_rowData"));
		FloatSalarySob FSalarySob = new FloatSalarySob();
		FSalarySob.setFloatSalarySobName(jObject.getString("floatSalarySobName"));
		FSalarySob.setBuildDate(new Date());
		FSalarySob.setBuilder(getSessionUserName(request));
		FSalarySob.setRemark(jObject.getString("remark"));
		JsonTool jTool = new JsonTool();
		try {
			floatSalaryService.saveSob(FSalarySob);
			jTool.setSuccess(true);
			jTool.setMsg("添加浮动工资账套【"+FSalarySob.getFloatSalarySobName()+"】成功！");
		} catch (Exception e) {
			jTool.setMsg("添加浮动工资账套【"+FSalarySob.getFloatSalarySobName()+"】失败！");
			e.printStackTrace();
		}
		writeJson(jTool, response);
	}
	
	@RequestMapping("updateSob")
	@ResponseBody
	public void updateSob(HttpServletRequest request,HttpServletResponse response) {
		JSONObject jObject = JSON.parseObject(request.getParameter("djt_FSsob_rowData"));
		FloatSalarySob FSalarySob = new FloatSalarySob();
		FSalarySob.setBuildDate(jObject.getDate("buildDate"));
		FSalarySob.setBuilder(jObject.getString("builder"));
		FSalarySob.setFloatSalarySobName(jObject.getString("floatSalarySobName"));
		FSalarySob.setRemark(jObject.getString("remark"));
		FSalarySob.setFloatSalarySobId(jObject.getIntValue("floatSalarySobId"));
		JsonTool jTool = new JsonTool();
		try {
			floatSalaryService.updateSob(FSalarySob);
			jTool.setSuccess(true);
			jTool.setMsg("编辑浮动工资账套【"+FSalarySob.getFloatSalarySobName()+"】成功！");
		} catch (Exception e) {
			jTool.setMsg("编辑浮动工资账套【"+FSalarySob.getFloatSalarySobName()+"】失败！");
			e.printStackTrace();
		}
		writeJson(jTool, response);
	}
	
	@RequestMapping("deleteSob")
	@ResponseBody
	public void deleteSob(HttpServletRequest request,HttpServletResponse response) {
		String ids = removeFirstAndLastChar(request.getParameter("djt_FSsob_rowData"));
		JsonTool jTool = new JsonTool();
		try {
			floatSalaryService.removeSob(ids);
			jTool.setSuccess(true);
			jTool.setMsg("删除浮动工资账套【成功】！");
		} catch (Exception e) {
			jTool.setMsg("删除浮动工资账套【失败】！");
			e.printStackTrace();
		}
		writeJson(jTool, response);
	}
	
	@RequestMapping("queryAllSob")
	@ResponseBody
	public List<FloatSalarySob> queryAllSob(){
		return floatSalaryService.querySobAll();
	}
	
	//--------------------浮动工资表--------------------------
	@RequestMapping("insertFS")
	@ResponseBody
	public void insertSS(HttpServletRequest request, HttpServletResponse response) {
		FloatSalary floatSalary = new FloatSalary();
		JsonTool jTool = new JsonTool();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date beginDate = sdf.parse(request.getParameter("beginDate"));
			floatSalary.setBeginDate(beginDate);
			String SendDate = request.getParameter("endDate");
			if (!(SendDate.equals(""))) {	
				Date endDate = sdf.parse(request.getParameter("endDate"));
				floatSalary.setEndDate(endDate);
			}
			floatSalary.setMender(getSessionUserName(request));
			floatSalary.setMendDate(new Date());
			String floatSalaryName = request.getParameter("floatSalaryName");
			floatSalary.setFloatSalaryName(floatSalaryName);
			Double value = Double.valueOf(request.getParameter("value"));
			floatSalary.setValue(value);
			String remark = request.getParameter("remark");
			floatSalary.setRemark(remark);
			int empId = Integer.valueOf(request.getParameter("empName"));
			floatSalary.setEmp(securityService.findEmpByID(empId));
			int floatSalarySobId = Integer.valueOf(request.getParameter("floatSalarySobId"));
			floatSalary.setFloatSalarySob(floatSalaryService.findSobByID(floatSalarySobId));
			floatSalaryService.saveFS(floatSalary);
			jTool.setSuccess(true);
			jTool.setMsg("添加浮动工资信息成功！");
		} catch (Exception e) {
			jTool.setMsg("添加浮动工资信息失败！");
			e.printStackTrace();
		}
		writeJson(jTool, response);
	}

	@RequestMapping("deleteFS")
	@ResponseBody
	public void deleteSS(HttpServletRequest request, HttpServletResponse response) {
		String ids = removeFirstAndLastChar(request.getParameter("djt_FS_rowData"));
		JsonTool jTool = new JsonTool();
		try {
			floatSalaryService.removeFS(ids);
			jTool.setSuccess(true);
			jTool.setMsg("删除员工浮动工资信息【成功】！");
		} catch (Exception e) {
			jTool.setMsg("删除员工浮动工资信息【失败】！");
			e.printStackTrace();
		}
		writeJson(jTool, response);
	}

	@RequestMapping("updateFS")
	@ResponseBody
	public void updateSS(HttpServletRequest request, HttpServletResponse response) {
		JSONObject jObject = JSON.parseObject(request.getParameter("djt_FS_rowData"));
		FloatSalary floatSalary = new FloatSalary();
		floatSalary.setFloatSalaryName(jObject.getString("floatSalaryName"));
		floatSalary.setValue(jObject.getDoubleValue("value"));
		floatSalary.setFloatSalaryId(jObject.getIntValue("floatSalaryId"));
		floatSalary.setFloatSalarySob(floatSalaryService.findSobByID(jObject.getIntValue("floatSalarySobId")));
		floatSalary.setBeginDate(jObject.getDate("beginDate"));
		floatSalary.setEndDate(jObject.getDate("endDate"));
		floatSalary.setMender(getSessionUserName(request));
		floatSalary.setMendDate(new Date());
		floatSalary.setRemark(jObject.getString("remark"));
		JsonTool jTool = new JsonTool();
		try {
			floatSalaryService.updateFS(floatSalary);
			jTool.setSuccess(true);
			jTool.setMsg("编辑【" + jObject.getString("empName") + "】的浮动工资信息成功！");
		} catch (Exception e) {
			jTool.setMsg("编辑【" + jObject.getString("empName") + "】的浮动工资信息失败！");
			e.printStackTrace();
		}
		writeJson(jTool, response);
	}

	@RequestMapping("findFSByPage")
	@ResponseBody
	public Map<String, Object> findSSByPage(int id, int page, int rows, String sort, String order) {
		Page<FloatSalaryVO> pages = floatSalaryService.queryFSByPage(id, page, rows, sort, order);
		Long total = pages.getTotalElements();
		List<FloatSalaryVO> list = pages.getContent();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	
}
